package com.eistert.java.datastructure._11_SegmentTree;

/**
 * @Description: 303.Range Sum Query - Immutable
 * @Author: ai
 * @create: 2023-04-05 18:17
 */
public class _303_RangeSumQueryImmutable {
}

class NumArray {


    private interface _11_Merger<E> {
        E merge(E a, E b);
    }

    /**
     * @Description: SegmentTree implementation code
     * @Author: ai
     * @create: 2023-04-05 15:47
     */
    private class _11_SegmentTree<E> {
        private E[] tree;
        private E[] data;
        // 定义线段树左右两个区间是如何融合的
        private _11_Merger<E> merger;

        public _11_SegmentTree(E[] arr, _11_Merger<E> merger) {

            this.merger = merger;

            data = (E[]) new Object[arr.length];
            for (int i = 0; i < arr.length; i++)
                data[i] = arr[i];

            /**
             * 结论，可以用等比数列推出来，这里记住这个结论就行：大小为n的数组，可以用长度4 * n的Tree表示。
             */
            tree = (E[]) new Object[4 * arr.length];
            buildSegmentTree(0, 0, arr.length - 1);
        }


        // 在treeIndex的位置创建表示区间[l...r]的线段树
        private void buildSegmentTree(int treeIndex, int l, int r) {

            if (l == r) {
                tree[treeIndex] = data[l];
                return;
            }


            // 计算treeIndex的左右孩子Index
            int leftTreeIndex = leftChild(treeIndex);
            int rightTreeIndex = rightChild(treeIndex);

            // int mid = (l + r) / 2;
            int mid = l + (r - l) / 2;
            buildSegmentTree(leftTreeIndex, l, mid);
            buildSegmentTree(rightTreeIndex, mid + 1, r);

            // 这里和业务相关，总之是综合左右两个节点的信息得到treeIndex节点的信息。
            tree[treeIndex] = merger.merge(tree[leftTreeIndex], tree[rightTreeIndex]);
        }

        public E get(int index) {
            if (index < 0 || index >= data.length)
                throw new IllegalArgumentException("Index is illegal.");
            return data[index];
        }

        public int getSize() {
            return data.length;
        }

        // 返回完全二叉树的数组表示中，一个索引所表示的元素的左孩子节点的索引
        private int leftChild(int index) {
            return 2 * index + 1;
        }

        // 返回完全二叉树的数组表示中，一个索引所表示的元素的右孩子节点的索引
        private int rightChild(int index) {
            return 2 * index + 2;
        }

        // 返回区间[queryL, queryR]的值
        public E query(int queryL, int queryR) {

            if (queryL < 0 || queryL >= data.length ||
                    queryR < 0 || queryR >= data.length || queryL > queryR)
                throw new IllegalArgumentException("Index is illegal.");

            return query(0, 0, data.length - 1, queryL, queryR);
        }

        // 在以treeIndex为根的线段树中[l...r]的范围里，搜索区间[queryL...queryR]的值
        private E query(int treeIndex, int l, int r, int queryL, int queryR) {

            if (l == queryL && r == queryR)
                return tree[treeIndex];

            int mid = l + (r - l) / 2;
            // treeIndex的节点分为[l...mid]和[mid+1...r]两部分

            int leftTreeIndex = leftChild(treeIndex);
            int rightTreeIndex = rightChild(treeIndex);

            // 搜索区间在treeIndex 的右边
            if (queryL >= mid + 1) {
                return query(rightTreeIndex, mid + 1, r, queryL, queryR);
                // 搜索区间是treeIndex的左边
            } else if (queryR <= mid) {
                return query(leftTreeIndex, l, mid, queryL, queryR);
            }

            // 搜索区间在treeIndex的左右两边
            E leftResult = query(leftTreeIndex, l, mid, queryL, mid);
            E rightResult = query(rightTreeIndex, mid + 1, r, mid + 1, queryR);
            return merger.merge(leftResult, rightResult);
        }
    }


    private _11_SegmentTree<Integer> segmentTree;

    public NumArray(int[] nums) {
        if (nums.length > 0) {
            Integer[] data = new Integer[nums.length];
            for (int i = 0; i < nums.length; i++) {
                data[i] = nums[i];
            }
            segmentTree = new _11_SegmentTree<>(data, (a, b) -> a + b);
        }

    }

    public int sumRange(int left, int right) {
        if (segmentTree == null) {
            return Integer.MAX_VALUE;
        }
        return segmentTree.query(left, right);
    }
}

/**
 * Your NumArray object will be instantiated and called as such:
 * NumArray obj = new NumArray(nums);
 * int param_1 = obj.sumRange(left,right);
 */
